#include "cBasic.h"
#include "ThirdBasics.h"
#define TRACE(...)					LG(## __VA_ARGS__)
#define TRACE_HEX(msg, pBuf, Len)	LG_HEX(msg, pBuf, Len)

void memcpy_u32(u32* pU1, u32* pU2, int len)
{
	while (len--)
		*pU1++ = *pU2++;
}

int memcmp_u32(u32* pU1, u32* pU2, int len)
{
	while (len--)
	{
		if (*pU1 ^ *pU2)
			return (*pU1 - *pU2);
		pU1++; pU2++;
	}
	return 0;
}

void memset_u32(u32* pU1, u32 tag, int len)
{
	while (len--)
		*pU1++ = tag;
}


void APP_fileSave(char* pFileName, void* pInBuff, int Inlen)
{
	FILE* f;
	errno_t err = fopen_s(&f, pFileName, "wb"); //(const char*)name
	if (err) {
		TRACE("��[%s]�ļ�[%d]ʧ��", pFileName, err);
		return;
	}
	size_t OutLen = fwrite(pInBuff, Inlen, 1, f);
	fclose(f);
	TRACE("fileSave->%s,Inlen[%d],WriteLen[%d]", pFileName, Inlen, OutLen);
}

void* APP_fileRead(char* pFileName, int* pOutLen)
{
	FILE* f;
	errno_t err = fopen_s(&f, pFileName, "rb"); //(const char*)name
	if (err) {
		TRACE("��[%s]�ļ�[%d]ʧ��", pFileName, err);
		return NULL;
	}
	fseek(f, 0, SEEK_END);
	int OutLen = ftell(f);
	if (OutLen == 0) {
		TRACE("In fopen_s->%s,RetLen Err", pFileName);
		return NULL;
	}
	char* pBuff = (char*)malloc(OutLen);
	fseek(f, 0, SEEK_SET);
	int ReadLen = fread(pBuff, OutLen, 1, f);
	fclose(f);
	TRACE("fileRead->%s,fileLen[%d],ReadLen[%d]", pFileName, OutLen, ReadLen);

	if (pOutLen)
		*pOutLen = ReadLen;
	return (void*)pBuff;
}

BYTE* APP_GetFileData(const char* pathName, int* pOutLen)
{
	FILE* f;
	errno_t err;
	err = fopen_s(&f, pathName, "rb"); //(const char*)name
	if (err == 0) {
		BYTE* pBfS = NULL;
		fseek(f, 0, SEEK_END);
		int OutLen = ftell(f);
		if (OutLen > 0) {
			fseek(f, 0, SEEK_SET);
			pBfS = (BYTE*)malloc(OutLen);
			fread(pBfS, 1, OutLen, f);
		}
		fclose(f);
		if (pOutLen) *pOutLen = OutLen;
		return pBfS;
	}
	//printf("open[%s]file,err[%d]", pathName, err);
	return NULL;
}

int APP_SetFileData(const char* pathName, BYTE* pBfS, int iLen)
{
	FILE* f;
	errno_t err = fopen_s(&f, pathName, "wb"); //(const char*)name
	if (err == 0) {
		int OutLen = fwrite(pBfS, 1, iLen, f);
		fclose(f);
		return OutLen;
	}
	else {
		printf("SetFileData[%s] Err[%d]", pathName, err);
	}
	return -1;
}

//==================================================================================================
#include <stdarg.h>
#include <time.h>
/*
void wCreateDir(wchar_t* pPath) 
{
	struct _stat64i32 st = { 0 }; //stat
	if (_wstat(pPath, &st) == -1) {
		if (_wmkdir(pPath)) {
			printf("文件夹创建失败！");
		}
	}
}
*/


